require 'rubygems'
require 'rake'
require 'rufus/scheduler'
require 'open-uri'
require 'action_mailer'

ActionMailer::Base.smtp_settings =
{:address => 'smtp.example.com',
  :domain  => 'example.com'}

class Mailer < ActionMailer::Base
  def badday_mailer(user)
    
    #standard ActionMailer message setup
    recipients  user.email
    from        "Votq Teams"
    subject     "Your Badday Stories Progress"
    #setting the body explicitly 
    template = ""
    template += "<html><body><div>Dear #{user.firstname}: <br><br>"
    template += "We would like to tell you about your badday stories progress."
    template += "Here are the progress about your badday stories:"
    user.baddays.each do |bd|
      if !bd.badday_emailer.blank?
        template += "<div>"
        template += "badday ID: #{bd.id}"
        template += "badday message: #{bd.message}"
        template += "badday agree: #{bd.agree}"
        template += "badday disagree: #{bd.disagree}"
        template += "</div>"
        template += "<br/>"
      end
    end
    template += "<br/>"
    template += "Thank You"
    template += "<br/>"
    template += "Votq Teams"
    
    body  template
  end
  
  def statement_mailer(user)
    
    #standard ActionMailer message setup
    recipients  user.email
    from        "Votq Teams"
    subject     "Your Statement Progress"
    #setting the body explicitly 
    template = ""
    template += "<html><body><div>Dear #{user.firstname}: <br><br>"
    template += "We would like to tell you about your statement progress."
    template += "Here are the progress about your statement:"
    user.statements.each do |s|
      if !s.statement_emailer.blank?
        template += "<div>"
        template += "statement ID: #{s.id}"
        template += "statement message: #{s.message}"
        template += "statement agree: #{s.agree}"
        template += "statement disagree: #{s.disagree}"
        template += "</div>"
        template += "<br/>"
      end
    end
    template += "<br/>"
    template += "Thank You"
    template += "<br/>"
    template += "Votq Teams"
    
    body  template
  end
  
  def question_mailer(user)
    
    #standard ActionMailer message setup
    recipients  user.email
    from        "Votq Teams"
    subject     "Your Question Progress"
    #setting the body explicitly 
    template = ""
    template += "<html><body><div>Dear #{user.firstname}: <br><br>"
    template += "We would like to tell you about your question progress."
    template += "Here are the progress about your question:"
    user.baddays.each do |q|
      if !bd.question_emailer.blank?
        template += "<div>"
        template += "question ID: #{q.id}"
        template += "question message: #{q.message}"
        template += "question agree: #{q.agree}"
        template += "question disagree: #{q.disagree}"
        template += "</div>"
        template += "<br/>"
        if q.comments.blank?
          q.comments.each do |c|
            template += "<div>"
            template += "answer Commentator: #{c.commentator}"
            template += "answer message: #{c.message}"
            template += "answer agree: #{c.agree}"
            template += "answer disagree: #{c.disagree}"
            template += "</div>"
            template += "<br/>"
          end
        end
      end
    end
    template += "<br/>"
    template += "Thank You"
    template += "<br/>"
    template += "Votq Teams"
    body  template
  end
end


scheduler = Rufus::Scheduler.start_new
scheduler.every("24h") do
  
  member = User.find(:all )
  member.each do |m|
    if !m.statements.blank?
      m.statements.each do |state|
        if !state.statement_emailer.blank?
          Mailer.deliver_statement_mailer(m)
          puts "send statement to #{m.email} ... success"
          break
        end
      end
    end
  end
end

scheduler.every("25h") do
  
  member = User.find(:all )
  member.each do |m|
    if !m.baddays.blank?
      m.baddays.each do |badday|
        if !badday.badday_emailer.blank?
          Mailer.deliver_badday_mailer(m)
          puts "send Badday Story to #{m.email} ... success"
          break
        end
      end
    end
  end
end

scheduler.every("26h") do
  member = User.find(:all )
  member.each do |m|
    if !m.questions.blank?
      m.questions.each do |q|
        if !q.question_emailer.blank?
          Mailer.deliver_question_mailer(m)
          puts "send Question to #{m.email} ... success"
          break
        end
      end
    end
  end
end



